const fs = require('fs')
const Koa = require('koa2')
const Router = require('koa-router');
const Pug = require('pug')
const serve = require('koa-static')
const app = new Koa()
const router = new Router()
const tpl = require('./tpl').pugTpl

const { getCourses } = require('./service/courses')
const port = 8081

router.get('/:course/:id', async (ctx, next) => {
  const { course, id } = ctx.params
  let result = await getCourses(course, id)
  
  ctx.body = Pug.render(tpl, {
    pages: result.pages,
    nav: result.nav,
    body: result.body
  })
})

app
  .use(serve(__dirname + '/public'))
  .use(router.routes())
  .use(router.allowedMethods())
  .listen(port)

console.log('http://localhost:'+ port)